//
//  SEGBasicTabBarViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/4.
//

#import "SEGBasicTabBarViewModel.h"
#import <SEGPlatform/SEGAdDisplayManager.h>
#import "SEGTabRequestManager.h"

@interface SEGBasicTabBarViewModel ()

@property (nonatomic, strong) SEGTabRequestManager *manager;

@end

@implementation SEGBasicTabBarViewModel

/// 请求广告展示配置
- (void)seg_requestAdConfig {
    // 请求广告展示配置(每次登录&切换小区的时候)
    [[SEGAdDisplayManager shared] requestNewConfig];
}
/// 请求Tab菜单
- (void)seg_requestMenuListCompletionHandler:(void(^)(SEGTabBarViewLayout *layout))completionHandler {
    SEGTabBarViewLayout *layout = [[SEGTabBarViewLayout alloc] initWithData:SEGMenuRequestManager.menuListTab type:SEGEnumTabBarViewType_NO];
    if (completionHandler) completionHandler(layout);
}
/// 请求是否需要修改密码
- (void)seg_requestUserWhetherNeedUpdatePasswordCompletionHandler:(void(^)(NSString *codeType))completionHandler {
    // 只有用户重新登录的时候请求，切换
    if ([SEGCommonConfig shared].isAlreadyAlterUesrChangePassword) return;
    [SEGCommonConfig shared].isAlreadyAlterUesrChangePassword = YES;
    [self.manager requestUserWhetherNeedUpdatePassword:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (completionHandler) completionHandler(codeType);
    }];
}
/// 提交暂不修改密码结果
- (void)seg_requestSubmitChangePasswordResult {
    [self.manager requestSubmitUserUpdatePasswordStatus:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
    }];
}
/// 用来判断是否是调用启动广告，默认NO
- (void)seg_initLaunchAdvertismentCompletionHandler:(void(^)(SEGAdvlistType position))completionHandler {
    NSNumber *isLaunchAd = [[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultLaunchAdvertisement];
    isLaunchAd = isLaunchAd ? isLaunchAd : @(NO);
    if ([isLaunchAd boolValue]) { //有启动广告，执行启动广告，弹出广告逻辑已经编写在启动广告关闭后
        if (completionHandler) completionHandler(SEGAdvlistType_START);
    } else {//没有启动广告，调用弹窗广告方法
        if (completionHandler) completionHandler(SEGAdvlistType_SYJ);
    }
}
/// tab开门成功处理逻辑
- (void)seg_tabBarOpenDoorSuccessedWithInfo:(NSDictionary *)info completionHandler:(void(^)(SEGAwardForwardType frowardType, NSDictionary *dicData))completionHandler {
    if ([info[@"type"] isEqualToString:@"AFTERFDOOROPEN"] && [info[@"doorStatus"] integerValue] == 1) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 一键开门积分行为设置
            [[SEGAnalyseBehaviorManager sharedManager] requestIntegralGrowthWithCode:SEGIntegralCode.openDoor finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            }];
            
            // 当开门成功后，判断是否有商业活动
            [[SEGAwardManager sharedManager] requestCheckActivityAfterOpenDoor:^(BOOL isSucceed, NSString *errorMessage, SEGAwardForwardType frowardType, NSDictionary *dicData) {
                if (isSucceed) {
                   // 回调
                   if (completionHandler) completionHandler(frowardType, dicData);
                }
            }];
        });
    }
}

#pragma mark - Getter
- (SEGTabRequestManager *)manager {
    if (!_manager) _manager = [[SEGTabRequestManager alloc] init];
    return _manager;
}

@end
